package com.shxc.smarttrade.index.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.github.nuptboyzhb.lib.SuperSwipeRefreshLayout;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseFragment;
import com.shxc.smarttrade.currency.activity.WebActivity;
import com.shxc.smarttrade.index.adapter.NoticeAdapter;
import com.shxc.smarttrade.index.model.NoticeModel;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisUtils;
import com.shxc.smarttrade.utils.MyOkHttpUtils;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 首页 - 公告
 */
@ContentView(R.layout.fragment_index_notice)
public class IndexNoticeFragment extends BaseFragment {

    @ViewInject(R.id.load_view)
    LinearLayout load_view;
    @ViewInject(R.id.loading_view)
    LinearLayout loading_view;
    @ViewInject(R.id.null_view)
    RelativeLayout null_view;
    @ViewInject(R.id.loadingfaild_view)
    LinearLayout loadingfaild_view;
    @ViewInject(R.id.superswiperefreshlayout)
    SuperSwipeRefreshLayout superswiperefreshlayout;
    @ViewInject(R.id.recyclerview)
    RecyclerView recyclerview;

    private List<NoticeModel.DataBean> list;
    private NoticeAdapter adapter;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        loading_view.setVisibility(View.VISIBLE);
        list = new ArrayList<>();
        adapter = new NoticeAdapter(mContext, list, handler);
        recyclerview.setAdapter(adapter);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        //设置滑动监听事件
        superswiperefreshlayout.setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                //TODO 开始刷新
                initData();
            }

            @Override
            public void onPullDistance(int distance) {
                //TODO 下拉距离
            }

            @Override
            public void onPullEnable(boolean enable) {
                //TODO 下拉过程中，下拉的距离是否足够出发刷新
            }
        });
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //點擊跳轉
                    NoticeModel.DataBean model = (NoticeModel.DataBean) msg.obj;
                    bundle.putString("title", "公告详情");
                    bundle.putString("url", Contents.H5PLUS_NOTICE + model.getId());
                    JumpTo(WebActivity.class, bundle);
//                    MobclickAgent.onEvent(getContext(), "transaction_detail");//友盟统计该点击事件发生次数
                    break;
            }
            super.dispatchMessage(msg);
        }
    };

    private void initData() {

        MyOkHttpUtils.getServiceDatas(null, Contents.ACTIVITY_CLIENTSHOWACTIVITY, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, NoticeModel.class);
                    if (jam.isSuccess()) {
                        NoticeModel model = (NoticeModel) jam.getData();
                        adapter.upDate(model.getData());
                        null_view.setVisibility(View.GONE);
                    } else {
                        null_view.setVisibility(View.VISIBLE);
                        loadingfaild_view.setVisibility(View.GONE);
                    }
                }
                load_view.setVisibility(View.GONE);
                superswiperefreshlayout.setRefreshing(false);
                superswiperefreshlayout.setLoadMore(false);
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                superswiperefreshlayout.setRefreshing(false);
                superswiperefreshlayout.setLoadMore(false);
                load_view.setVisibility(View.VISIBLE);
                loadingfaild_view.setVisibility(View.VISIBLE);
                loadingDialog.dismiss();
            }
        });
    }
}
